import Vue from 'vue'
import App from '@/App'
import config from './config.js';
import request from './request.js';
var dateUtils = require('@/common/util.js').dateUtils;

// 处理参数
const setData = function(items, calTime) {
	if (items == undefined)
		return [];
	// this.show(items)
	var newItems = [];
	items.forEach(e => {
		e.pic = config.staticUrl + e.pic;
		if (calTime)
			e.time = dateUtils.humanize(new Date(e.time).getTime());
		newItems.push(e);
	});
	return newItems;
}

const goDetail = function(e, _this) {
	// console.log(e)
	var Id = e.Id + ".html";
	if (e.typeId == "chanpin")
		_this.$Router.push({
			name: 'detail-chanpin',
			params: {
				id: Id
			}
		})
	else if (e.typeId == "shipin")
		_this.$Router.push({
			name: 'detail-v',
			params: {
				id: Id
			}
		})
	else
		_this.$Router.push({
			name: 'detail',
			params: {
				id: Id
			}
		})
}

const goPage = function(e) {
	uni.navigateTo({
		url: e
	});
}
// 设置分页参数
function setPageData(type) {
	// console.log(type)
	request.api_request('page_data', {
		type: type
	}).then(function(data) {
		// console.log(data);
		data = data.result;
		if (data != undefined) {
			setTDK(data.title, data.description, data.keywords);
		}
		// 取消刷新图标
		uni.stopPullDownRefresh();
	})
}

// tdk
const setTitle = function(e) {
	var title = e + '___' + config.title;
	uni.setNavigationBarTitle({
		title: title
	})
	document.title = title;
	document.getElementsByName('title')[0].content = title;
}
const setDescription = function(e) {
	document.getElementsByName('description')[0].content = e;
	document.getElementsByName('description')[1].content = e;
}
const setKeywords = function(str) {
	document.getElementsByName("keywords")[0].content = str;
}
const setTDK = function(title, des, keywords) {
	setTitle(title);
	setDescription(des);
	setKeywords(keywords);
}
// 设置主页参数
function setIndexData() {
	request.api_request('index_data').then(function(data) {
		// console.log(data);
		var data_layout = data.result.layout;
		config.links = data.result.link;
		config.title = data_layout.title;
		config.banner = data.result.banner;
		setTDK(data_layout.title, data_layout.description, data_layout.keywords);

		// 取消刷新图标
		uni.stopPullDownRefresh();
	})
}
const setCache = function(key, data) {
	uni.setStorage({
		key: key,
		data: data,
		success: (res) => {
			console.log('setCache成功')
		},
		fail: () => {
			console.log('setCache失败')
		}
	})
}

const getCache = function(key) {
	return new Promise(function(resolve, reject) {
		uni.getStorage({
			key: key,
			success: (res) => {
				// console.log("cache:" + res.data)
				resolve(res.data);
			},
			fail: (rt) => {
				// 浏览器缓存清理后的容错处理
				setCache(key, false);
				console.log(rt)
				// 触发浏览器报错
				// reject(rt)
			}
		});
	})
}

const delCache = function(key) {
	uni.removeStorageSync(key)
	console.log('数据重置成功');
}

const alert = function(str) {
	uni.showToast({
		title: str,
		icon: "none"
	});
}

export default {
	setData,
	goDetail,
	goPage,
	setTitle,
	setDescription,
	setKeywords,
	setTDK,
	setIndexData,
	setPageData,
	setCache,
	getCache,
	delCache,
	alert
}
